package com.ylbh.modules.reply.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ylbh.common.pojo.reply.dto.AddOrModifyReplyDTO;
import com.ylbh.common.pojo.reply.dto.CommunalDTO;
import com.ylbh.common.utils.IdWorker;
import com.ylbh.common.utils.PageUtils;
import com.ylbh.common.utils.ShiroUtils;
import com.ylbh.common.utils.UploadHtmlFile;
import com.ylbh.modules.reply.entity.ShoppingReply;
import com.ylbh.modules.reply.mapper.ShoppingReplyMapper;
import com.ylbh.modules.reply.service.ShoppingReplyService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Service
public class ShoppingReplyServiceImpl extends ServiceImpl<ShoppingReplyMapper , ShoppingReply> implements ShoppingReplyService {

    @Autowired
    private ShoppingReplyMapper shoppingReplyMapper;

    @Override
    public PageUtils queryReplyList(CommunalDTO replyDTO) {

        Page<Object> page = new Page<>(replyDTO.getPage(), replyDTO.getLimit());
        List<ShoppingReply> list = shoppingReplyMapper.selectReplyList(replyDTO, page);
        Page<ShoppingReply> page1 = new Page<>();
        page1.setRecords(list).setTotal(page.getTotal());
        return new PageUtils(page1);
    }

    @Override
    public void addNewReply(AddOrModifyReplyDTO addOrModifyReplyDTO) {
        ShoppingReply reply = new ShoppingReply();
        BeanUtils.copyProperties(addOrModifyReplyDTO,reply);
        IdWorker idW = new IdWorker();
        Date date = new Date();
        reply.setReplyId(idW.nextId());
        reply.setCreateTime(date);
        reply.setCreateUserId(ShiroUtils.getUserId().toString());
        reply.setUpdateTime(date);
        reply.setUpdateUserId(ShiroUtils.getUserId().toString());
        if (StringUtils.isNotBlank(reply.getContent())) {
            reply.setContent(reply.getContent().replace("<video class", "<video playsinline webkit-playsinline class"));
            reply.setContent(reply.getContent().replace("&tp=webp", ""));
            String url = UploadHtmlFile.uploadHtmlOss(reply.getContent(), "reply_info_file");
            reply.setRichContent("https://file.yilianbaihui.cn/reply_info_file/" + url);
        }
        shoppingReplyMapper.insertReply(reply);
    }

    @Override
    public ShoppingReply getReplyById(String replyId) {
        return shoppingReplyMapper.selectById(replyId);
    }

    @Override
    public void removeReplyByIds(List<String> list) {
        shoppingReplyMapper.deleteReply(list);
    }

    @Override
    public void modifyReplyEnableStateById(CommunalDTO communalDTO) {
        shoppingReplyMapper.updateEnableStateById(communalDTO);
    }

    @Override
    public void modifyReplyById(AddOrModifyReplyDTO addOrModifyReplyDTO) {
        ShoppingReply reply = new ShoppingReply();
        BeanUtils.copyProperties(addOrModifyReplyDTO,reply);
        reply.setUpdateTime(new Date());
        reply.setUpdateUserId(ShiroUtils.getUserId().toString());
        if (StringUtils.isNotBlank(reply.getContent())) {
            reply.setContent(reply.getContent().replace("<video class", "<video playsinline webkit-playsinline class"));
            reply.setContent(reply.getContent().replace("&tp=webp", ""));
            String url = UploadHtmlFile.uploadHtmlOss(reply.getContent(), "reply_info_file");
            reply.setRichContent("https://file.yilianbaihui.cn/reply_info_file/" + url);
        }
        shoppingReplyMapper.updateReplyById(reply);
    }

    @Override
    public void modifyReplyEnableStateByIds(String batchValue, String[] ids) {
        shoppingReplyMapper.updateReplyEnableStateByIds(batchValue, Arrays.asList(ids));
    }

    @Override
    public void modifyReplyRecommendStateById(CommunalDTO communalDTO) {
        shoppingReplyMapper.updateReplyRecommendStateById(communalDTO);
    }
}
